package edu.uic.cs.i422.s2011.team4;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SquirrelUIGenerator implements OnClickListener {
	
	private Activity tRef;
	
	private int title;
	private int help;
	private int bread;
	
	private Toast toast;
	private int toastYOffset;
	private boolean toastActive;
	
	View header;
	
	/**
	 * All we need is the Activity reference, to construct our widgets
	 * @param t
	 */
	public SquirrelUIGenerator(Activity t) {
		tRef = t;
		title = 0;
		help = 0;
		bread = 0;
		toastYOffset = 0;
	}
	
	/**
	 * This returns our header generated View.
	 * @return
	 */
	public View getHeader() {
		/**
		 *  Load our header template 
		 */
		header = View.inflate(tRef, R.layout.header, null);		
		
		/**
		 * Set the Title
		 */
		TextView t = (TextView) header.findViewById(R.id.headerTitle);
		t.setText(title);
		
		/**
		 * Set the Bread Crumb
		 */
		TextView c = (TextView) header.findViewById(R.id.headerBreadCrumb);
		c.setText(bread);
		
		/**
		 * Fix the help button a little bit
		 */
		ImageButton b = (ImageButton) header.findViewById(R.id.headerHelp);
		b.setBackgroundResource(0);
		b.setOnClickListener(this);
		b.setPadding(0,2,5,2);
        
        /**
         * return the container containing all the views in this class
         * generated.
         */
        return header;
	}
	
	public void setTitle(int i) {
		title = i;
	}
	
	public void setHelp(int i) {
		help = i;
	}
	public void setBreadCrumb(int i) {
		bread = i;
	}

	public void onClick(View v) {
		if (v.getId() == R.id.headerHelp && !toastActive) {				
			/** Produce the toast; */
			toastYOffset = header.getBottom() - header.getHeight()/5;
			/**
			 *  Produce our costume Toast */
			LinearLayout layout = (LinearLayout) View.inflate(tRef, R.layout.squirrel_toast, null);					
			
			/* Set the text */
			TextView text = (TextView) layout.findViewById(R.id.squirrelToastText);
			text.setText(help);
			
			toast = new Toast(tRef);
			toast.setGravity(Gravity.TOP, 0, toastYOffset);
			toast.setDuration(20);
			toast.setView(layout);
			toast.show();
			toastActive = true;
		} else if (v.getId() == R.id.headerHelp && toastActive) {
			toast.cancel();
			toastActive = false;
		}
	}
}
